/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Prototype;

import java.io.*;
import java.lang.String.*;

/**
 *
 * @author Pales
 */
public class FileInt {

    ProtoController proto;
    FileReader fr;
    BufferedReader br;
    String comfile;
    Boolean endof;
    String[] words;

    FileInt(ProtoController proto) {
        this.proto = proto;
    }

    public void fileinterpret(String file) {
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            endof = true;
            while (endof) {

                comfile = br.readLine();
                if (comfile != null) {
                    String[] words = {"bad"};
                    command com = command.BAD;



                    try {
                        words = comfile.split(" ");
                    } catch (Exception e) {
                        System.out.println("Bad Command!");
                    }

                    if (words.length != 0) {
                        if (words[0].equals("addLed")) {
                            com = command.ADDLED;
                        }
                        if (words[0].equals("addGate")) {
                            com = command.ADDGATE;
                        }
                        if (words[0].equals("help")) {
                            com = command.HELP;
                        }
                        if (words[0].equals("addOscill")) {
                            com = command.ADDOSCILL;
                        }
                        if (words[0].equals("addGateToComp")) {
                            com = command.ADDGATETOCOMP;
                        }
                        if (words[0].equals("addInverterToComp")) {
                            com = command.ADDINVERTERTOCOMP;
                        }
                        if (words[0].equals("addButton")) {
                            com = command.ADDBUTTON;
                        }
                        if (words[0].equals("addComposite")) {
                            com = command.ADDCOMPOSIT;
                        }
                        if (words[0].equals("addInverter")) {
                            com = command.ADDINVERTER;
                        }
                        if (words[0].equals("addRategen")) {
                            com = command.ADDRATEGEN;
                        }
                        if (words[0].equals("addWireGG")) {
                            com = command.ADDWIREGG;
                        }
                        if (words[0].equals("addChildWire")) {
                            com = command.ADDCHILDWIRE;
                        }
                        if (words[0].equals("addWireIG")) {
                            com = command.ADDWIREIG;
                        }
                        if (words[0].equals("compAddWireIG")) {
                            com = command.COMPADDWIREIG;
                        }
                        if (words[0].equals("compAddWireGG")) {
                            com = command.COMPADDWIREGG;
                        }
                        if (words[0].equals("compAddWireGO")) {
                            com = command.COMPADDWIREGO;
                        }
                        if (words[0].equals("loadFile")) {
                            com = command.LOADFILE;
                        }
                        if (words[0].equals("clean")) {
                            com = command.CLEAN;
                        }
                        if (words[0].equals("delWire")) {
                            com = command.DELETEWIRE;
                        }
                        if (words[0].equals("delete")) {
                            com = command.DELETE;
                        }
                        if (words[0].equals("pause")) {
                            com = command.PAUSE;
                        }
                        if (words[0].equals("run")) {
                            com = command.RUN;
                        }
                        if (words[0].equals("setButton")) {
                            com = command.SETBUTTON;
                        }
                        if (words[0].equals("stop")) {
                            com = command.STOP;
                        }
                        if (words[0].equals("getValue")) {
                            com = command.GETVALUE;
                        }
                        if (words[0].equals("step")) {
                            com = command.STEP;
                        }
                        if (words[0].equals("exit")) {
                            com = command.EXIT;
                        }
                    }
                    switch (com) {
                        case HELP:
                            InterCommands.help();
                            break;
                        case ADDOSCILL:
                            InterCommands.addOscill(proto, words);
                            break;
                        case ADDLED:
                            InterCommands.addLed(proto, words);
                            break;
                        case ADDBUTTON:
                            InterCommands.addButton(proto, words);
                            break;
                        case ADDCOMPOSIT:
                            InterCommands.addComposite(proto, words);
                            break;
                        case ADDINVERTER:
                            InterCommands.addInverter(proto, words);
                            break;
                        case ADDGATE:
                            InterCommands.addGate(proto, words);
                            break;
                        case ADDGATETOCOMP:
                            InterCommands.addGateToComp(proto, words);
                            break;
                        case ADDINVERTERTOCOMP:
                            InterCommands.addInverterToComp(proto, words);
                            break;
                        case ADDRATEGEN:
                            InterCommands.addRategen(proto, words);
                            break;
                        case ADDWIREGG:
                            InterCommands.addWireGG(words, proto);
                            break;
                        case ADDWIREIG:
                            InterCommands.addWireIG(words, proto);
                            break;
                        case ADDCHILDWIRE:
                            InterCommands.addChildWire(words, proto);
                            break;
                        case COMPADDWIREIG:
                            InterCommands.compAddWireIG(words, proto);
                            break;
                        case COMPADDWIREGG:
                            InterCommands.compAddWireGG(words, proto);
                            break;
                        case COMPADDWIREGO:
                            InterCommands.compAddWireGO(words, proto);
                            break;
                        case STOP:
                            InterCommands.stop(proto, words);
                            break;
                        case LOADFILE:
                            InterCommands.loadFile(proto, words);
                            break;
                        case CLEAN:
                            InterCommands.clean(proto, words);
                            break;
                        case DELETE:
                            InterCommands.delete(words, proto);
                            break;
                        case DELETEWIRE:
                            InterCommands.delWire(words, proto);
                            break;
                        case PAUSE:
                            InterCommands.pause(proto, words);
                            break;
                        case RUN:
                            InterCommands.run(proto, words);
                            break;
                        case SETBUTTON:
                            InterCommands.setbutton(words, proto);
                            break;
                        case GETVALUE:
                            InterCommands.getValue(words, proto);
                            break;
                        case STEP:
                            InterCommands.step(words, proto);
                            break;
                        default:
                            System.out.println("unknown command");
                            break;

                    }
                } else {
                    endof = false;
                }
            }


            br.close();
        } catch (java.io.IOException e) {
            System.out.println(e.toString());
        }


    }
}
